package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.system.service.domain.CoolUserDetails;
import cn.da.shuai.cool.system.model.entity.user.CoolUser;
import cn.da.shuai.cool.system.service.service.ICoolUserService;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Slf4j
@AllArgsConstructor
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final ICoolUserService coolUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StrUtil.isBlank(username)) {
            return null;
        }

        CoolUser coolUser = coolUserService.getByUsername(username);
        if (coolUser == null) {
            return null;
        }

        return new CoolUserDetails(coolUser, AuthorityUtils.createAuthorityList(coolUserService.getMenuPermsById(coolUser.getId())));
    }
}



